package com.tencent.wxcloudrun.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson2.JSONObject;
import io.github.admin4j.http.HttpRequest;
import io.github.admin4j.http.core.Pair;
import io.github.admin4j.http.util.HttpUtil;
import okhttp3.Response;

/**
 * @author Ryan Zhao
 *         <p>
 *         Copyright: Copyright (c) 2023
 *         </p>
 * @version 1.0
 * @date 2023-08-26 16:30
 * @description: http请求工具类
 */
public class OkHttpUtil {

    public static JSONObject get(String url, Map<String, Object> queryParams) throws IOException {
        Response response = HttpUtil.get(url, queryParams);
        return JSONObject.parseObject(response.body().string());
    }

    public static JSONObject get(String url, Map<String, Object> queryParams, Map<String, Object> headers) throws IOException {
        HttpRequest httpRequest = HttpRequest.get(url);
        setParams(queryParams, httpRequest);
        Response response = httpRequest.queryParams().headers(headers).execute();
        return JSONObject.parseObject(response.body().string());
    }

    public static JSONObject post(String url, String json) throws IOException {
        Response response = HttpUtil.post(url, json);
        assert response.body() != null;
        return JSONObject.parseObject(response.body().string());
    }

    public static JSONObject postForm(String url, Map<String, Object> formParams) throws IOException {
        Response response = HttpUtil.postForm(url, formParams);
        assert response.body() != null;
        return JSONObject.parseObject(response.body().string());
    }

    public static JSONObject post(String url, String json, Map<String, Object> headers) throws IOException {
        HttpRequest httpRequest = HttpRequest.post(url);
        httpRequest.setBody(json);
        Response response = httpRequest.headers(headers).execute();
        return JSONObject.parseObject(response.body().string());
    }

    private static void setParams(Map<String, Object> queryParams, HttpRequest httpRequest) {
        List<Pair> pairs = new ArrayList<>(queryParams.size());
        queryParams.forEach((x, y) -> pairs.add(Pair.of(x, y)));
        if (pairs.size() > 0) {
            pairs.forEach(httpRequest::queryParams);
        }
    }

    private static JSONObject upload() throws IOException {
        File file = new File("C:\\Users\\andanyang\\Downloads\\Sql.txt");
        Map<String, Object> formParams = new HashMap<>();
        formParams.put("key", "test");
        formParams.put("file", file);
        formParams.put("token",
            "WXyUseb-D4sCum-EvTIDYL-mEehwDtrSBg-Zca7t:qgOcR2gUoKmxt-VnsNb657Oatzo=:eyJzY29wZSI6InpoYW56aGkiLCJkZWFkbGluZSI6MTY2NTMwNzUxNH0=");
        Response response = HttpUtil.upload("https://upload.qiniup.com/", formParams);
        return JSONObject.parseObject(response.body().string());
    }

    private static void download() throws IOException {
        HttpUtil.down("https://gitee.com/admin4j/common-http", "path/");
    }

}
